import { createApp } from 'vue'
import App from './App.vue'
import './assets/main.css'
import { router } from './router/index.js'

import { createPinia } from 'pinia'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

import HelperAxios from 'axios'
import HelperNewAxios from 'axios'
import reqFuncAxios from './until/axiosInstance.js'

const pinia = createPinia();
const AppNew = createApp(App);
AppNew.config.globalProperties.$countriesData = [];//全局属性

////请求拦截器
HelperNewAxios.interceptors.request.use(function (config) {
    //config axios实例
    config.baseURL = 'http://localhost:46208';
    switch (config.method) {
        case 'put':
        case 'post':
            config.data.PublicMsg = '请求拦截器post请求 添加的请求数据';
            break;
        case 'delete':
        case 'get':
            config.params.PublicMsg = '请求拦截器get请求 添加的请求数据';
            break;
    }
    config.headers.authorization = 'kbPpDmex2xNn6eN47AJTp2FEncQHcaWp';
    return config;
}, function (error) { return Promise.reject(error) })

////响应拦截器
HelperNewAxios.interceptors.response.use(
    function (response) {
        response.data.PublicMsg = '响应拦截器';
        //预设网络异常代码
        const jsonHttpcode = {
            A100: 'Continue 继续',
            A101: 'Switching Protocols 切换协议',
            A400: 'Bad Request 客户端请求的语法错误',
            A401: 'Unauthorized 请求要求用户的身份认证',
            A403: 'Forbidden ',
            A404: 'Not Found'
        }
        //预设接口异常代码
        const jsonApiCode = {
            Fail: 'This is fail',
            Error: 'This is error'
        }
        if (response.status === 200) {
            if (response.data.StatusCode === "Success") {
                return response.data
            }
            else {
                alert(jsonApiCode[response.data.StatusCode])
            }
        }
        else {
            alert(jsonHttpcode['A' + response.status])
        }
        return response
    }, function (error) { return Promise.reject(error) })

AppNew.config.globalProperties.$HelperNewAxios = HelperNewAxios;
AppNew.config.globalProperties.$HelperAxios = HelperAxios;//将axios作为一个全局属性
AppNew.config.globalProperties.$reqFuncAxios = reqFuncAxios;//二次封装axios


AppNew.use(router);
AppNew.use(ElementPlus);
AppNew.use(pinia);
AppNew.mount('#app');
